package com.baizhi.cmfzmanager.util;

import com.baizhi.cmfzmanager.entity.Article;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneUtil {

    public static FSDirectory fsDirectory;
    public static IKAnalyzer analyzer;
    static{
        try {
            fsDirectory=FSDirectory.open(new File("D://lucenedir"));
            analyzer=new IKAnalyzer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 封装Document对象
     * @return
     */
    public static Document addArticle(Article article){
        Document document = new Document();
        document.add(new TextField("articleId",article.getArticleId().toString(), Field.Store.YES));
        document.add(new TextField("articleName",article.getArticleName(), Field.Store.YES));
        document.add(new TextField("articleImage",article.getArticleImage(), Field.Store.YES));
        document.add(new TextField("articleContext",article.getArticleContent(), Field.Store.YES));
        return document;


    }

    /**
     * 获取索引写出对象
     * @return
     */
    public static IndexWriter getIndexWriter()  {
        IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_44,analyzer);
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(fsDirectory,indexWriterConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWriter;
    }


    /**
     * 处理关键词
     * @param keyword
     * @return
     */
    public static Query getQuery(String keyword){
        String[] field = {"articleId","articleName","articleContent"};
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44,field,analyzer);
        Query query = null;
        try {
            query = queryParser.parse(keyword);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return query;
    }

    /**
     * 获取查询对象
     * @return
     */
    public static IndexSearcher getIndexSearch(){
        IndexReader indexReader = null;
        IndexSearcher indexSearcher=null;
        try {
            indexReader= DirectoryReader.open(fsDirectory);
            indexSearcher = new IndexSearcher(indexReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexSearcher;
    }

    /**
     * 提交方法
     * @param indexWriter
     */
    public static void commit(IndexWriter indexWriter){
        try {
            indexWriter.commit();
        } catch (IOException e) {
            e.printStackTrace();
            try {
                indexWriter.rollback();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }finally {
            try {
                indexWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
